package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author FangKun
 * @create 2024-04-10-14:55
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        Long userId = BaseContext.getCurrentId();
        // 判断数据库是否存在该记录
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishFlavor != null, ShoppingCart::getDishFlavor, dishFlavor);
        ShoppingCart shoppingCart = baseMapper.selectOne(shoppingCartLambdaQueryWrapper);

        if (shoppingCart != null){
            // 如果存在数量 + 1
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            baseMapper.updateById(shoppingCart);
        } else {
            // 如果不存在则添加
            ShoppingCart cart = new ShoppingCart();
            cart.setNumber(1);
            cart.setUserId(userId);
            cart.setCreateTime(LocalDateTime.now());
            //添加时判断是套餐还是菜品，补全信息
            if (dishId != null){
                // 走到这里说明是菜品
                Dish dish = dishMapper.selectById(dishId);
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
                cart.setDishFlavor(dishFlavor);
                cart.setDishId(dishId);
            } else {
                // 走到这里说明是套餐
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setSetmealId(setmealId);
                cart.setAmount(setmeal.getPrice());
            }
            baseMapper.insert(cart);
        }

    }

    @Override
    public List<ShoppingCart> shoppingCartList() {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = baseMapper.selectList(shoppingCartLambdaQueryWrapper);
        return shoppingCartList;
    }

    @Override
    public void cleanShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        baseMapper.delete(shoppingCartLambdaQueryWrapper);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //根据参数查询对应的购物车商品
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishFlavor != null, ShoppingCart::getDishFlavor, dishFlavor);
        ShoppingCart shoppingCart = baseMapper.selectOne(shoppingCartLambdaQueryWrapper);

        // 如果数量 > 1,数量减1
        if (shoppingCart.getNumber() > 1){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            baseMapper.updateById(shoppingCart);
        } else {
            // 否则，直接删除该记录
            baseMapper.deleteById(shoppingCart.getId());
        }
    }
}















